package com.zm.devmag.user.server.Impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zm.devmag.user.server.UsersServer;
import com.zm.devmag.util.MD5Util;
import com.zm.devmag.user.dao.UsersMapper;
import com.zm.devmag.user.po.Users;
@Service
public class UsersServerImpl implements UsersServer {
	@Autowired
	private UsersMapper userMapper;
	
	@Override
	public List<Users> findAll(String username, String nickname) {
		// TODO Auto-generated method stub
		Map<String,Object> param = new HashMap<String,Object>();
		if(!StringUtils.isEmpty(username)){
			param.put("username", username);
		}
		if(!StringUtils.isEmpty(nickname)){
			try {
				nickname = URLEncoder.encode(nickname,"utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			param.put("nickname", nickname);
		}
		
		return userMapper.findAll(param);
	}
	
	public Users selectByPrimaryKey(Long id){
		return userMapper.selectByPrimaryKey(id);
	} 
	
	public int updateByUserData(Users user){
		Users oldu = userMapper.selectByPrimaryKey(user.getUid());
		String ups= user.getPassw();
		System.out.println(MD5Util.Encode_B64_MD5(ups));
		user.setPassw(MD5Util.Encode_B64_MD5(ups));
		user.setUtype(oldu.getUtype());
    	return userMapper.updateByPrimaryKeySelective(user);
    }
	public int insertSelective(Users record){
		String ups= record.getPassw();
		record.setPassw(MD5Util.Encode_B64_MD5(ups));
		record.setUtype("2");
		record.setCreattime(new Timestamp(System.currentTimeMillis()));
		System.out.println(MD5Util.Encode_B64_MD5(ups));
		return userMapper.insertSelective(record);
	}
	public int deleteByPrimaryKey(Long uid){
		return userMapper.deleteByPrimaryKey(uid);
	}
	public Users SelectUserByUacc(String uacc){
		return userMapper.SelectUserByUacc(uacc);
	}
}
